import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2022-12-29
 * Time: 13:30
 */
public class Solution {
    // https://leetcode.cn/problems/roman-to-integer/
    public static int romanToInt(String s) {
        Map<Character, Integer> map = new HashMap<>();
        map.put('I', 1);
        map.put('V', 5);
        map.put('X', 10);
        map.put('L', 50);
        map.put('C', 100);
        map.put('D', 500);
        map.put('M', 1000);

        int ret = 0;
        int i = 0;
        while (i < s.length()-1) {
            // 当前左边的罗马数字大于等于右边的罗马数字
            if (map.get(s.charAt(i)) >= map.get(s.charAt(i+1))) {
                ret += map.get(s.charAt(i));
                i++;
                continue;
            }
            // 当前左边的罗马数字小于右边的罗马数字
            if (map.get(s.charAt(i)) < map.get(s.charAt(i+1))) {
                ret += map.get(s.charAt(i+1));
                ret -= map.get(s.charAt(i));
                i+=2;
            }
        }
        if (i == s.length()-1) {
            ret += map.get(s.charAt(s.length()-1));
        }
        return ret;
    }

    // 测试
    public static void main(String[] args) {
        System.out.println(romanToInt("MCMXCIV"));
    }

    // https://leetcode.cn/problems/remove-element/
    public static int removeElement(int[] nums, int val) {
        int l = 0;
        int r = nums.length - 1;
        while (l <= r) {
            while (l <= r && nums[r] == val) {
                r -= 1;
            }
            if (l <= r && nums[l] == val) {
                // 交换
                int tmp = nums[l];
                nums[l] = nums[r];
                nums[r] = tmp;
            }
            if (nums[l] == val) {
                return l;
            }
            l++;
        }
        return l;
    }

    // 测试
    public static void main1(String[] args) {
        int[] arr = {1};
        System.out.println(removeElement(arr, 1));
        System.out.println();
    }
}
